package edu.umd.cs.mouseprecision;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import edu.umd.cs.mouseprecision.TaskCanvas.TaskType;

/**
 * Represents a data file that has been captured during one task
 * @author Nico
 *
 */
public class DataFile {
	
	private double[][] traveledPoints;
	
	private TaskType taskType;
	
	private long timestamp;
	
	private double usedTime;
	
	private long traveledPointsCount;

	public DataFile(double usedTime, TaskType taskType, double[][] traveledPoints, long traveledPointsCount){
		this.usedTime = usedTime;
		this.taskType = taskType;
		this.traveledPoints = traveledPoints;
		this.traveledPointsCount = traveledPointsCount;
		
	}
	
	
	
	public DataFile(String filepath){
		
		File file = new File(filepath);
	    FileInputStream fis = null;
	    BufferedInputStream bis = null;
	    DataInputStream dis = null;

	    try {
			  fis = new FileInputStream(file);
			  bis = new BufferedInputStream(fis);
			  dis = new DataInputStream(bis);
			
			  //first line: time stamp 
			  if(dis.available() != 0){
				  timestamp=Long.parseLong(dis.readLine());
			  }
			  
			  //second line: task type
			  if(dis.available() != 0){
				  taskType=taskType.valueOf(dis.readLine());
			  }
			  
			  //third line: time in seconds
			  if(dis.available() != 0){
				  usedTime=Double.parseDouble(dis.readLine());
			  }
			  
			  //rest points
			  ArrayList<double[]> temp = new ArrayList<double[]>();
			  while (dis.available() != 0) {
			    String[] splitted = dis.readLine().split(" ");
			    double[] temp2 = new double[3];
			    temp2[0] = Double.parseDouble(splitted[1]);
			    temp2[1] = Double.parseDouble(splitted[2]);
			    temp2[2] = Double.parseDouble(splitted[3]);
			    temp.add(temp2);
			  }
			  
			  traveledPoints=new double[temp.size()][3];
			  int i = 0;
			  for(double[] t : temp){
				  traveledPoints[i++]=t;
	  
			  }
			  
			
			  fis.close();
			  bis.close();
			  dis.close();

	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
		
	}
	
	public void writeToDisk(){
		long currentTime= System.currentTimeMillis();
		
		BufferedWriter out;
		try{
		   out = new BufferedWriter(new FileWriter(currentTime+"_"+taskType.toString()+".data"));
		   
		   //first line: timestamp
		   out.write(new Long(currentTime).toString());
		   
		   //second line: task type
		   out.write("\n");
		   out.write(taskType.toString());
		   
		   //third line: time in seconds
		   out.write("\n");
		   out.write(new Double(usedTime).toString());
		   
		   //write mouse points measurements
		   for(int i =0; i<traveledPointsCount;i++){
			   out.write("\n"+i+" "+traveledPoints[i][0]+" "+traveledPoints[i][1]+" "+traveledPoints[i][2]);
		   } 
		   out.close();
		}
		catch(IOException e){
		   e.printStackTrace();
		}
	}
	
	public double[][] getTraveledPoints() {
		return traveledPoints;
	}

	public void setTraveledPoints(double[][] traveledPoints) {
		this.traveledPoints = traveledPoints;
	}

	public TaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public double getUsedTime() {
		return usedTime;
	}

	public void setUsedTime(double usedTime) {
		this.usedTime = usedTime;
	}
	

}
